<?php
  /**
   * This class implement methods to interact with an uploaded file.<br />
   *
   * @author Dany Mottier
   * @version 3.0
   */
  class PostedFile extends Base {
    /** @var string The name of the uploaded file. */
    private $filename = "";

    /**
     * Store the filename in private variable.<br />
     *
     * @param string $fieldname The filename.
     *
     * @see <a href="http://fr2.php.net/manual/en/function.is-string.php">is_string()</a>
     */
    public function __construct($filename) {
      if (is_string($filename))
        $this->filename = $filename;
      else
        throw new WebException("\$filename parameter should be a string");
    }

    /**
     * Returns the simple name of the file.<br />
     *
     * @return string The filename.<br /><br />
     *
     * @see <a href="http://fr2.php.net/manual/en/function.basename.php">basename()</a>
     */
    public function getBaseName() {
      return basename($_FILES[$this->filename]['name']);
    }

    /**
     * Returns the name of the file.<br />
     *
     * @return string The name of the file.
     */
    public function getName() {
      return $_FILES[$this->filename]['name'];
    }

    /**
     * Returns the type of the file.<br />
     * 
     * @return string The type of the file.
     */
    public function getType() {
      return $_FILES[$this->filename]['type'];
    }

    /**
     * Returns the temporary associated name of the file.<br />
     * This method is an alias of UploadedFile::getTmpName().<br />
     *
     * @return string The temporary associated name of the file.<br /><br />
     *
     * @see <a href="#">getTmpName()</a>
     */
    public function getTemporaryName() {
      return $this->getTmpName();
    }

    /**
     * Returns the temporary associated name of the file.<br />
     *
     * @return string The temporary associated name of the file.<br /><br />
     */
    public function getTmpName() {
      return $_FILES[$this->filename]['tmp_name'];
    }

    /**
     * Returns the error if exists.<br />
     *
     * @return string The error.
     */
    public function getError() {
      return $_FILES[$this->filename]['error'];
    }

    /**
     * Returns the size of the file.<br />
     * 
     * @return integer The size of the file.
     */
    public function getSize() {
      return $_FILES[$this->filename]['size'];
    }

    /**
     * Define if there is error.<br />
     *
     * @return boolean TRUE if there is an error, FALSE otherwise.
     */
    public function hasError() {
      $error_code = $_FILES[$this->filename]['error'];
      
      if ($error_code == 0)
        return FALSE;
      else
        return TRUE;
    }

    /**
     * Define if the uploaded file exists.<br />
     *
     * @return boolean TRUE if the uploaded file exists, FALSE otherwise.
     */
    public function exists() {
      return isset($_FILES[$this->filename]);
    }

    /**
     * Moves the uploaded file to a new location.<br />
     * This public function checks to ensure that the file designated by filename is a valid uploaded file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination. This sort of check is especially important if there is any chance that anything done with uploaded files could reveal their contents to the user, or even to other users on the same system.<br />
     *
     * @param $destination The destination of the moved file.
     * @param $filename If given, rename the uploaded file.
     *
     * @return boolean Returns FALSE if the file does not exist or if an error occured.<br /><br />
     *
     * @see <a href="http://be.php.net/manual/en/public function.move-uploaded-file.php">move_uploaded_file()</a>
     */
    public function Move($destination, $filename = "") {
      if (is_string($destination)) {
        if (is_string($filename)) {
          if (! empty($filename))
            return move_uploaded_file($_FILES[$this->filename]['tmp_name'],
                                      $destination . $filename);
          else
            return move_uploaded_file($_FILES[$this->filename]['tmp_name'],
                       $destination . $_FILES[$this->filename]['name']);
        }
      }
    }
  }
?>
